// 1. 发布订阅


class EventEmitter {
    constructor(){
        this.events = {};
    }

    on(event, callback){
        if(!this.events[event]) this.events[event] = [];
        this.events[event].push(callback)
    }
    emit(event, ...args){
        this.events[event].forEach((cb) => cb(...args))
    }
    off(event, callback){
        this.events[event].filter((cb) => cb !== callback)
    }
    once(event,callback){
        this.on(event, wrapFn)
        function wrapFn(...args){
            this.off(event, wrapFn)
            callback.call(this, ...args)
        }
    }   
}